<?php
namespace app\controller;
use think\App;

class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;
    protected $sitedb;
    protected $stroperate;

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        //初始化内部封装DB类
        $this->sitedb=new \mylibrary\Database();
        $this->stroperate=new \mylibrary\StringOperate();

        $this->app     = $app;
        $this->request = $app->request;

        $this->sitedb->map = [];
        if(session("member_id")){
            $this->sitedb->map_eq("id",session("member_id"));
            $info = $this->sitedb->find("member");
            $this->sitedb->map = [];
            $data['member_info']=$info;
        }else{
            $data['member_info']=null;
        }
        \think\facade\View::assign($data);
    }

    /**
     * 操作成功跳转的快捷方法
     * @access protected
     * @param  mixed     $msg 提示信息
     * @param  mixed     $data 返回的数据
     * @param  string    $url 跳转的URL地址
     * @return void
     */
    protected function success($msg = '', $data = [], string $url = '')
    {
        $result = [
            'code' => 1,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'time' => date("Y-m-d H:i:s"),
        ];
        if($this->request->isJson() || $this->request->isAjax()){
            return json($result);
        }else{
            return redirect($url);
        }
    }
    /**
     * 操作错误跳转的快捷方法
     * @access protected
     * @param  mixed     $msg 提示信息
     * @param  mixed     $data 返回的数据
     * @param  string    $url 跳转的URL地址
     * @return void
     */
    protected function error($msg = '', $data = [], string $url = '')
    {
        $result = [
            'code' => 0,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'time' => date("Y-m-d H:i:s"),
        ];
        if($this->request->isJson() || $this->request->isAjax()){
            return json($result);
        }else{
            return redirect($url);
        }
    }
}
